<template>
  <j-modal :title="title" :width="1500" :visible="visible" :maskClosable="false" switchFullscreen @ok="handleOk"
    :okButtonProps="{ class: { 'jee-hidden': false } }" @cancel="handleCancel">
    <scmxjd-form ref="realForm" @ok="submitCallback" :disabled="disableSubmit" />
  </j-modal>
</template>

<script>
import { postAction } from '../../../../../api/manage'

import ScmxjdForm from './ScmxjdForm'

export default {
  name: 'ScmxjdModal',
  components: {
    ScmxjdForm
  },
  data () {
    return {
      title: '',
      width: 2000,
      visible: false,
      disableSubmit: false
    }
  },
  methods: {
    add () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs.realForm.add();
      })
    },
    edit (record) {
      this.visible = true
      this.$nextTick(() => {
        this.$refs.realForm.edit(record);
      })
    },
    close () {
      this.$emit('close');
      this.visible = false;
    },
    handleOk () {
      // this.$refs.realForm.handleOk();
      //提交去顶
      console.log("handleok");
      // if(this.title === '查看报价'){
      //   this.$message.error("已报价，不允许再次报价");
      //   return;
      // }
      let data = this.$refs.realForm.scmxjditemTable.dataSource;
      let ids = [];
      //判断选择的供应商是哪个？

      //找对对应的所选择的供应商
      console.log(this.$refs.realForm.activeKey);

      let vendorid = this.$refs.realForm.activeKey;
      if (vendorid == undefined || vendorid == '') {
        this.$message.error("未选择供应商来下单");
        return;
      }

      //判断是否同一物料不同品质选择多个
      let selectedValue = [];
      let selectedItemIds = [];
      for (let i = 0; i < data.length; i++) {
        let curRow = data[i]
        if (!curRow.hasOwnProperty('checkinfo')) {
          continue;
        }
        if (curRow.checkinfo == false) {
          continue;
        }
        ids.push(curRow["quoid"]);
        if (selectedValue.length == 0) {
          selectedValue.push(curRow['itemid']);
          selectedItemIds.push(curRow['parentid'] + "&" + curRow["id"]);
          continue;
        }
        for (let j = 0; j < selectedItemIds.length; j++) {
          if (selectedItemIds[j] == (curRow['parentid'] + "&" + curRow["id"])) {
            this.$message.error("一个配件不允许选择多个品质来下单");
            return;
          }
        }
      }

      if (ids.length === 0) {
        this.$message.error("请先选择一条报价信息来下达订单");
        return;
      }
      postAction('/rfq.po/scmpo/generatepo', { id: ids }).then(res => {
        if (res.success) {
          this.$message.success("下达生成订单成功");
          this.visible = false;
        } else {
          this.$message.error(res.message);
        }
      })

    },
    getAllTable () {
      if (!(this.refKeys instanceof Array)) {
        throw this.throwNotArray('refKeys')
      }
      let values = this.refKeys.map(key => getRefPromise(this, key))
      return Promise.all(values)
    },

    submitCallback () {
      // this.$emit('ok');
      // this.visible = false;
      console.log("submitcallback");
    },
    handleCancel () {
      this.close()
    }
  }
}
</script>

<style scoped></style>